﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;

using Quartz;
using Quartz.Impl;
using NUnit.Framework;


namespace ShootSearch.Tests.Core
{
    public class SpiderTest: IJob
    {
        #region IJob Members

        public void Execute(JobExecutionContext context)
        {
            Debug.WriteLine("Spider.Start");

            Debug.WriteLine("Spider.Config=" + context.MergedJobDataMap["Config"]);

            Thread.Sleep(5000);


            Debug.WriteLine("Spider.End");

            JobDetail detail2 = new JobDetail("Parser", "GP2", typeof(ParserTest));
            detail2.JobDataMap.Put("Config", "PARSER_GP2");
            SimpleTrigger trigger2 = new SimpleTrigger("trigger2", "GP2");


            context.Scheduler.ScheduleJob(detail2, trigger2);

        }

        #endregion
    }

    public class ParserTest : IJob
    {
        #region IJob Members

        public void Execute(JobExecutionContext context)
        {
            Debug.WriteLine("Parser.Start");
            Debug.WriteLine("Parser.Config=" + context.MergedJobDataMap["Config"]);

            Thread.Sleep(5000);

            Debug.WriteLine("Parser.End");

        }

        #endregion
    }

    
    public class JobLauncher
    {
        //List<JobDetail> jobs = new List<JobDetail>();

        protected ISchedulerFactory factory;
        protected IScheduler scheduler;


        public JobLauncher()
        {}

        public void Launch()
        {
            Debug.WriteLine("Launch.Start");
            JobDetail detail1 = new JobDetail("Spider", "GP1", typeof(SpiderTest));
            detail1.JobDataMap.Put("Config", "SPIDER_GP1");
            SimpleTrigger trigger1 = new SimpleTrigger("trigger1", "GP1" );




            factory = new StdSchedulerFactory();
            scheduler = factory.GetScheduler();
            scheduler.ScheduleJob(detail1, trigger1);
            //scheduler.ScheduleJob(detail2, trigger2);
            scheduler.Start();


            Thread.Sleep(15000);
            Debug.WriteLine("Launch.End");

        }
    }

    [TestFixture]
    public class JobLauncherTest
    {
        [Test]
        public void Test()
        {
            JobLauncher l = new JobLauncher();
            l.Launch();
            
        }

        public static void Run()
        {
            new JobLauncherTest().Test();
        }
    }
}
